package com.module.lemlin.base

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.*
import com.module.lemlin.R
import com.module.lemlin.view.LoginAuthPopup
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.EventBus.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import rxhttp.RxHttp

abstract class BaseAbstractActivity : AppCompatActivity() {

    /**
     * 需要绑定的视图ID
     */
    abstract val layoutResId: Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(layoutResId)
        getDefault().register(this)
        this.init()
    }

    override fun onStop() {
        super.onStop()
        //隐藏软键盘
        if (KeyboardUtils.isSoftInputVisible((this@BaseAbstractActivity)))
            KeyboardUtils.hideSoftInput((this@BaseAbstractActivity))
    }

    override fun onDestroy() {
        super.onDestroy()
        getDefault().unregister(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onEventMainThread(any: Any) {
    }

    /**
     * 初始化操作
     * */
    private fun init() {
        this.initStatusBar()
        this.doBeforeInit()
        this.initView()
        this.initData()
    }

    fun createViewEmptyNull(hint: String): View {
        return layoutInflater.inflate(R.layout.view_empty_null, null).apply {
            findViewById<TextView>(R.id.tvEmptyNullHint).text = hint
        }
    }

    fun createViewEmptyLoad(): View {
        return layoutInflater.inflate(R.layout.view_empty_load, null)
    }

    fun createViewFooter(hint: String = "没有更多数据"): View =
        TextView(this as Context).apply {
            layoutParams = FrameLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                ConvertUtils.dp2px(30f)
            )
            gravity = Gravity.CENTER
            textSize = 13f
            text = hint
            setTypeface(null, Typeface.BOLD)
            setTextColor(Color.parseColor("#CCCCCC"))
        }

    /**
     * 设置沉浸式状态栏
     * */
    private fun initStatusBar() {
        //设置状态栏模式
        BarUtils.setStatusBarLightMode(
            (this), stateBar().isLightMode
        )
        //设置状态栏颜色
        BarUtils.setStatusBarColor(
            (this), ColorUtils.getColor(stateBar().color)
        )
        //为标题栏添加状态栏的高度
        stateBar().view?.let {
            BarUtils.addMarginTopEqualStatusBarHeight(it)
        }
        //设置底部菜单栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            BarUtils.setNavBarColor(this.window, ColorUtils.getColor(stateBar().bottomColor))
        }
    }

    /**
     * 初始化视图和数据之前进行操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化视图
     */
    open fun initView() = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit

    /**
     * toast提示
     * */
    fun toast(code: Int? = 0, message: String?) {
        if (code != 401)
            ToastUtils.showShort(message)
    }

    /**
     *
     * */
    open fun stateBar(): StatusBar = StatusBar()

    data class StatusBar(
        var isLightMode: Boolean = true,
        @ColorRes var color: Int = android.R.color.transparent,
        @ColorRes var bottomColor: Int = android.R.color.white,
        var view: View? = null
    )
}